<?php
namespace app\admin\controller;
use app\admin\validate\MenuValidate;
use think\exception\ValidateException;
use app\service\MenuService;
use app\model\Menu as MenuModel;
class Menu extends Admin
{
    public function __construct(MenuService $service)
    {
        parent::__construct();
        $this->service = $service;
    }
    /**
     * 菜单列表
     * @return string|\think\response\Json
     */
    public function index()
    {
        if (!$this->request->isPost()) {
            $top_list = $this->service->getTopList();
            $this->assign("title","菜单列表");
            $this->assign("top_list",$top_list);
            return $this->fetch("index");
        }
        $pid = $this->request->post("pid",0);
        $menus = $this->service->listToTree(["status"=>MenuModel::MENU_STATUS_ACTIVE],$pid);
        $data["rows"]["total"] =1;
        $data["rows"]["list"] = $menus;
        $this->result($data,0,"获取成功");
    }

    /**
     * 保存菜单
     * @param $params
     * @return mixed
     * @throws ValidateException
     */

    private function save($params)
    {
          $level = 0;
          if (!empty($params["pid"])) {
              $menuInfo = $this->service->getOne(["id"=>$params["pid"]]);
              if (!$menuInfo) {
                  throw new  ValidateException("参数错误");
              }
              $level = $menuInfo["level"]+1;
          }
          $params["level"] = $level;
          return $this->service->save($params);
    }

    /**
     * 添加菜单
     * @return string|\think\response\Json
     * @throws ValidateException
     */
    public function add()
    {
        if (!$this->request->isPost()) {
            $menus = $this->service->listToTree(["is_menu"=>1,"status"=>MenuModel::MENU_STATUS_ACTIVE]);
            $menus = array_merge(array(0=>array('id'=>0,'title_show'=>'顶级菜单')),$menus);
            $this->assign("menus",$menus);
            $this->assign("vo",null);
            return $this->fetch("form");
        }
        $params = $this->request->Post();
        $params["status"] = MenuModel::MENU_STATUS_ACTIVE;
        try {
            validate(MenuValidate::class)->scene("add")->check($params);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }
        $this->save($params) ? $this->success("添加成功"):$this->error("保存失败");
    }

    /**
     * 编辑菜单
     * @return string|\think\response\Json
     * @throws ValidateException
     */
    public function edit()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get("id");
            if (!$id) {
                throw new ValidateException("缺少id");
            }
            $menus = $this->service->listToTree(["is_menu"=>1,"status"=>MenuModel::MENU_STATUS_ACTIVE]);
            $menus = array_merge(array(0=>array('id'=>0,'title_show'=>'顶级菜单')),$menus);
            $this->assign("menus",$menus);
            $vo =  $this->service->get($id);
            $this->assign("vo",$vo);
            return $this->fetch("form");
        }
        $params = $this->request->Post();
        try {
            validate(MenuValidate::class)->scene("edit")->check($params);
        } catch (ValidateException $e) {
             $this->error($e->getError());
        }
        $this->save($params) ? $this->success("编辑成功"):$this->error("保存失败");
    }

    /**
     * 菜单状态修改
     * @return \think\response\Json
     */
    public function status()
    {
        $params = $this->request->Post();
        try {
            validate(MenuValidate::class)->scene("status")->check($params);
        } catch (ValidateException $e) {
          $this->error($e->getError());
        }
        $ret = $this->service ->setStatus($params["id"],$params["status"]);
        if ($ret) {
              $this->success("设置成功");
        } else {
             $this->error("设置失败");
        }
    }
}